

<!DOCTYPE html>

<!--
http://www.prepbootstrap.com/license

All the Templates and Themes on this website can be used under the MIT license, stated below. Some templates and themes can use third-party components and frameworks, that are subject to their own licenses.

Copyright (c) 2014 PrepBootstrap.com
Alterations made by abetusk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-->

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MeowCAD</title>

    <link rel="shortcut icon" href="/simpleoutline.ico" />
    <link rel="icon" href="/simpleoutline.ico" />

    <!-- <script type='text/javascript' src='/socket.io/socket.io.js' ></script> -->
    <script type='text/javascript' src='/js/lib/socket.io.js' ></script>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>

    <script type='text/javascript' src='js/meowmeow.js' ></script>

    <!-- <link rel="stylesheet" type="text/css" href="bootstrap/css/pastel-stream.css" /> -->
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.css" />

    <link rel="stylesheet" type="text/css" href="/css/buttons.css" />
    <link rel="stylesheet" type="text/css" href="/css/forms.css" />
    <link rel="stylesheet" type="text/css" href="/css/tables.css" />

    <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>

    <!--spinner-->
    <link rel="stylesheet" href="/css/ladda-themeless.css">
    <script src="/js/lib/spin.js"></script>
    <script src="/js/lib/ladda.js"></script>
    <!--spinner-->

    <script>

      var g_url = "/";

      $(document).ready( function() {
        meowmeow();

        $("#username").keydown( function(e) {
          //tab
          if ((e.keyCode == 9) && (!e.shiftKey)) { 
            $("#password").focus(); return false; 
          }
          return true;
        });

        $("#username").keyup( function(e) {
          // carriage return
          if (e.keyCode == 13) { $("#password").focus(); }
        });

        $("#password").keydown( function(e) {
          if ((e.keyCode == 9) && (e.shiftKey)) { 
            $("#username").focus();
            return false;
          }
          return true;
        });

        $("#password").keyup( function(e) {
          if (e.keyCode == 13) { tryLogin(); }
        });

        $("#buttonSignin").keydown( function(e) {
          if (e.keyCode == 9) {
            $("#forgot").focus();
            return false;
          }
          return true;
        });

        $("#forgot").keydown( function(e) { 
          console.log(e);
          if (e.keyCode == 9) {
            $("#forgot").blur();
            return false;
          }
          return true; 

        });


      });

      function clearMessage() {
        var msg = document.getElementById("message");
        msg.className = "";
        msg.innerHTML = "";
        //msg.style.height = "100px";

        var filler = document.getElementById("filler");
        filler.style.height = "80px";
      }

      function successMessage( text )
      {
        var msg = document.getElementById("message");
        msg.className = "alert alert-success";
        msg.innerHTML = text;
        //msg.style.height = "";

        var filler = document.getElementById("filler");
        filler.style.height = "0px";

      } var messagesDisplayed=0;
      function errorMessage( text )
      {
        var msg = document.getElementById("message");
        //msg.style.height = "";

        //msg.className = "alert alert-danger fade in";

        if (messagesDisplayed==0) {
          msg.className = "alert alert-danger";
        } else {
          msg.className = "alert alert-danger fade";
          setTimeout( function() { 
            $("#message").addClass("in");
          }, 100);
        }

        messagesDisplayed++;

        msg.innerHTML = text;

        var filler = document.getElementById("filler");
        filler.style.height = "0px";



      }

      function clearErrorHints() 
      {
        var u = document.getElementById("usernameDiv");
        u.className = "form-group";
        var p = document.getElementById("passwordDiv");
        p.className = "form-group";
      }

      function errorHint(name) {
        var b = document.getElementById(name);
        b.className = "form-group has-error";
      }

      function disableButton() {
        var btn = document.getElementById( 'buttonSignin' );
        var l = Ladda.create(btn);
        l.start();
      }

      function enableButton() {
        var btn = document.getElementById( 'buttonSignin' );
        var l = Ladda.create(btn);
        l.stop();
      }

      function tryLogin()
      {
        console.log("tryLogin");

        clearErrorHints();
        clearMessage();
        disableButton();

        var username = document.getElementById("username");
        var password = document.getElementById("password");

        var uname = username.value;
        var pword = password.value;

        if (uname.length == 0)
        {
          errorMessage("Please provide Username");
          errorHint("usernameDiv");
          enableButton();
          username.focus();
          return;
        }

        if (pword.length == 0)
        {
          errorMessage("Please provide Password");
          errorHint("passwordDiv");
          enableButton();
          password.focus();
          return;
        }

        if (typeof io !== 'undefined')
        {
          var socket = io.connect( g_url );

          messagesDisplayed=0;

          socket.on("disconnect", function() { console.log("disconnected"); });
          socket.on("connect_failed", function() { console.log("connect_failed"); });
          socket.on("reconnect_failed", function() { console.log("reconnect_failed"); });
          socket.on("error", function() {
            console.log("error");
            errorMessage("Server error, please try again later!  Sorry for the inconvenience!");
            enableButton();
          });

          socket.emit("auth", { username : username.value, password : password.value });
          socket.on("auth", function(dat) {
            console.log("auth response, got:");
            console.log(dat);

            enableButton();

            if ( (dat.type != "response") || (dat.status != "success") )
            {
              errorMessage("Authentication failed");
              return;
            }
            else
            {
              successMessage("success!");

              $.cookie("userId", dat.userId , { path:'/', secure: true} );
              $.cookie("sessionId", dat.sessionId, { path:'/', secure:true} );
              $.cookie("userName" , uname , { path:'/', secure:true} );

              console.log("cookie: " + $.cookie("sessionToken") );


              // redirect...
              //
              window.location.href = g_url + "portfolio";

              // I don't think we want to use this, but just in case...
              //window.location.replace( g_url + "/bleepsix/bleepsix_sch.html");

            }
          });

        }
        else {
          errorMessage("Server offline, please try again later!  Sorry for the inconvenience!");
          enableButton();
          return;
        }

      }


    </script>


</head>

<body>


  <!--NAVBAR-->


  <div class="container">


    <!--MESSAGE-->
    <!-- <div id='message' style='height:100px;' ></div> -->

    <div id='message' ></div>
    <div id='filler' style='height:80px;'></div>


    <div class='row'>

      <div class='col-xs-2'></div>

      <div class='col-xs-8' >

        <div class='panel panel-success'>

          <div class='panel-heading'>Log in</div>
          <div class='panel-body'>

              <div class='form-group' id='usernameDiv'>
                <label >Username</label>
                <input id='username' type="text" class='form-control' placeholder="Username" ></input>
              </div>

              <label id='forgotLabel' >Password <a id='forgot' href='/forgot'><small>(forgot password?)</small></a> </label>

              <div class='form-group' id='passwordDiv' >
                <input id='password' type="password" class='form-control' placeholder="Password">
              </div>

              <div class='form-group' >
                <button id='buttonSignin' class='btn btn-success ladda-button' data-style='zoom-out' onclick='tryLogin();'>
                  <span class='ladda-label'> 
                    Log in
                  </span>
                </button>
              </div>


          </div>
        </div>

      </div>

      <div class='col-xs-2'></div>

    </div>


  <!--FOOTER-->

  </div> <!-- container -->

  <!--ANALYTICS-->


</body>
</html>

